package com.example.service;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.AirLane;

import com.example.entity.BaseAirLane;
import com.example.entity.BaseTakeoffLand;
import com.example.entity.BaseWork;
import com.example.mapper.BaseAirLaneMapper;
import com.example.mapper.BaseWorkMapper;
import com.example.service.interfaces.BaseAirLaneInterface;
import com.example.service.interfaces.BaseWorkInterface;
import com.example.trans.Wp;
import com.example.trans.transBaseTakeoffLand;
import com.example.trans.transBaseWork;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @program: microservice
 * @description:
 * @author: Aprilies
 * @create: 2024-09-10 16:51
 **/

@Service
public class BaseWorkService extends ServiceImpl<BaseWorkMapper, BaseWork> implements BaseWorkInterface {

    @Autowired
    private BaseWorkMapper baseWorkMapper ;

    public List<transBaseWork> findAll(){
        List<BaseWork> baseWorks = baseWorkMapper.selectList(null);
        return baseWorks.stream().map(baseWork -> {
            transBaseWork transBaseWork = new transBaseWork();
            BeanUtils.copyProperties(baseWork, transBaseWork);
            String wp = baseWork.getWaypointsCoordinates();
            System.out.println(wp);
            if(wp != null && !wp.isEmpty()) {
                JSONArray jsonArray = JSONArray.parseArray(wp);
                List<Wp> workWp = jsonArray.toJavaList(Wp.class);
                System.out.println(workWp);
                transBaseWork.setWaypointsCoordinates(workWp);
            }
            return transBaseWork;
        }).collect(Collectors.toList());
    }

    public Optional<transBaseWork> findByID(Integer id) {
        BaseWork baseWork = baseWorkMapper.selectById(id);
        if (baseWork == null) {
            return Optional.empty();
        }
        transBaseWork transBaseWork = new transBaseWork();
        BeanUtils.copyProperties(baseWork, transBaseWork);
        // 单独处理 waypointsCoordinates 字段
        String wp = baseWork.getWaypointsCoordinates();
        if (wp != null && !wp.isEmpty()) {
            JSONArray jsonArray = JSONArray.parseArray(wp);
            List<Wp> workWp = jsonArray.toJavaList(Wp.class);
            transBaseWork.setWaypointsCoordinates(workWp);
        }
        return Optional.of(transBaseWork);
    }

    public transBaseWork isave(transBaseWork transBaseWork) {
        BaseWork baseWork = new BaseWork();
        // 使用 BeanUtils 复制属性
        BeanUtils.copyProperties(transBaseWork, baseWork);

        // 将 List<AirLaneWp> 转换为 JSON 字符串并设置到 waypointsCoordinates
        List<Wp> waypoints = transBaseWork.getWaypointsCoordinates();
        if (waypoints != null && !waypoints.isEmpty()) {
            String wpJson = JSONArray.toJSONString(waypoints);
            baseWork.setWaypointsCoordinates(wpJson);
        }
        // 插入数据
        baseWorkMapper.insert(baseWork);
        transBaseWork.setId(baseWork.getId());
        return transBaseWork;
    }

    // 只返回更新的字段数据
    public Optional<transBaseWork> update(transBaseWork user) {
        BaseWork baseWork = new BaseWork();
        BeanUtils.copyProperties(user, baseWork);
        List<Wp> waypoints = user.getWaypointsCoordinates();
        if (waypoints != null && !waypoints.isEmpty()) {
            String wpJson = JSONArray.toJSONString(waypoints);
            baseWork.setWaypointsCoordinates(wpJson);
        }
        int i = baseWorkMapper.updateById(baseWork);
        if(i == 1){
            return Optional.of(user);
        }
        return Optional.empty();
    }

    public boolean delete(Integer id) {
        if (baseWorkMapper.selectById(id) != null) {
            baseWorkMapper.deleteById(id);
            return true;
        }
        return false;
    }

}
